package gttp.nspa.client.adm;

import gttp.nspa.client.adm.activity.BillingListActivity;
import gttp.nspa.client.adm.activity.CustomerFormActivity;
import gttp.nspa.client.adm.activity.CustomerListActivity;
import gttp.nspa.client.adm.place.BillingListPlace;
import gttp.nspa.client.adm.place.CustomerListPlace;
import gttp.nspa.client.adm.place.CustomerPlace;

import com.google.gwt.activity.shared.Activity;
import com.google.gwt.activity.shared.ActivityMapper;
import com.google.gwt.place.shared.Place;
import com.google.inject.Inject;
import com.google.inject.Provider;

public class NsActivityMapper implements ActivityMapper {

    @Inject
    private Provider<BillingListActivity>  billingAct;

    @Inject
    private Provider<CustomerFormActivity> custFormAct;

    @Inject
    private Provider<CustomerListActivity> custListAct;

    private Place                          lastPlace;
    private Activity                       lastActivity;

    public Activity getActivity(Place place) {
        if (place.equals(lastPlace)) {
            return lastActivity;
        }

        lastPlace = place;
        if (place instanceof BillingListPlace) {
            BillingListActivity billList = billingAct.get();
            billList.withPlace((BillingListPlace) place);
            lastActivity = billList;
        } else if (place instanceof CustomerPlace) {
            CustomerFormActivity custForm = custFormAct.get();
            custForm.withPlace((CustomerPlace) place);
            lastActivity = custForm;
        } else if (place instanceof CustomerListPlace) {
            CustomerListActivity custList = custListAct.get();
            custList.withPlace((CustomerListPlace) place);
            lastActivity = custList;
        }
        return lastActivity;
    }
}
